Quick Sort
Example 1:
Input:
N = 5
arr[] = { 4, 1, 3, 9, 7}
Output:
1 3 4 7 9
Code
#include <iostream>
using namespace std;
int partition (int arr[], int low, int high)
{
int pivot = arr[high];
int i = low;
int temp;
for (int j = low; j <= high- 1; j++)
{
if (arr[j] <=pivot)
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
i++;
}
}
temp=arr[i];
arr[i]=arr[high];
arr[high]=temp;
return (i);
}
void quickSort(int arr[], int low, int high)
{
if (low <high)
{
int pi = partition(arr, low, high);
quickSort(arr, low, pi - 1);
quickSort(arr, pi + 1, high);
}
}
int main()
{
int i,n,arr[100];
cin>>n;
for(i=0;i>arr[i];
quickSort(arr, 0, n-1);
printf("Sorted array: \n");
for(i=0;i<n;i++)
cout<<arr[i]<<" ";
return 0;
}